/**
 * 
 */
package org.pexx.components.logger;

import java.text.SimpleDateFormat;
import java.util.Calendar;


/**
 * 
 */
public class Log {
	
	private static Log log = null;
	
	//
	
	private StringBuffer logBuffer;
	private int lineCount = 0;
		
	private String eol = "\r\n";	
	private String separator = " ";	
	private String messagePrefix = ""; 
	private String warningPrefix = "W";
	private String errorPrefix = "E";
	private String exceptionPrefix = "E";
	
	private SimpleDateFormat df = new SimpleDateFormat("[H:m:s]");
	/**
	 * 
	 */
	private Log() {		
		
		 logBuffer = new StringBuffer();
	}	
	
	/**
	 * @return the log
	 */
	public static Log getLog() {
		
		if (log == null)
			log = new Log();

		return log;
	}
	
	public void logMessage (String message) {
		
		// append prefix
		
		logBuffer.append(messagePrefix);		
		
		appendTime();		
		
		logBuffer.append(message);
		
		appendLineEnd();
	}

	public void logWarning (String message) {
		
		// append prefix
		
		logBuffer.append(warningPrefix);		
		
		appendTime();		
		
		logBuffer.append(message);
		
		appendLineEnd();
	}

	public void logError (String message) {
		
		// append prefix
		
		logBuffer.append(errorPrefix);		
		
		appendTime();		
		
		logBuffer.append(message);
		
		appendLineEnd();
	}
	
	public void logException (Exception e) {
		logError(e.getClass().getSimpleName());
		logError(e.getCause().getMessage());
	}
	
	public void log (String message) {
		logMessage(message);
	}
	
	public void log (Exception e) {
		logException(e);
	}
	
	/**
	 * 
	 */
	private void appendLineEnd() {

		logBuffer.append(eol);
		
		lineCount++;
		
	}
	

	/**
	 * 
	 */
	private void appendTime() {

//		logBuffer.append('[');
//		logBuffer.append(calendar.get(Calendar.HOUR_OF_DAY));		
//		logBuffer.append(":");
//		logBuffer.append(calendar.get(Calendar.MINUTE));
//		logBuffer.append(":");
//		logBuffer.append(calendar.get(Calendar.SECOND));
//		logBuffer.append(']');
		
		logBuffer.append(df.format(Calendar.getInstance().getTime()));
		
		logBuffer.append(separator);
		
	}

	public void clear() {
		logBuffer.setLength(0);
		lineCount = 0;
	}
	
	/**
	 * @return the lineCount
	 */
	public int getLineCount() {

		return lineCount;
	}
	
	@Override
	public String toString () {
		return logBuffer.toString();
	}

	
	public String getEol() {
	
		return eol;
	}

	
	public void setEol(String eol) {
	
		this.eol = eol;
	}

	
	public String getSeparator() {
	
		return separator;
	}

	
	public void setSeparator(String separator) {
	
		this.separator = separator;
	}

	
	public String getMessagePrefix() {
	
		return messagePrefix;
	}

	
	public void setMessagePrefix(String messagePrefix) {
	
		this.messagePrefix = messagePrefix;
	}

	
	public String getWarningPrefix() {
	
		return warningPrefix;
	}

	
	public void setWarningPrefix(String warningPrefix) {
	
		this.warningPrefix = warningPrefix;
	}

	
	public String getErrorPrefix() {
	
		return errorPrefix;
	}

	
	public void setErrorPrefix(String errorPrefix) {
	
		this.errorPrefix = errorPrefix;
	}

	
	public String getExceptionPrefix() {
	
		return exceptionPrefix;
	}

	
	public void setExceptionPrefix(String exceptionPrefix) {
	
		this.exceptionPrefix = exceptionPrefix;
	}


	
}
